<template>
  <div class="books">
    <el-row :gutter="20">
      <el-col :span="24">
        <div style="margin-bottom: 10px; text-align: right;">
          <el-button type="primary" @click="addNewUser">添加新用户</el-button>
        </div>
        <el-table
            :data="tableData"
            style="width: 100%">
          <el-table-column
              prop="uid"
              label="用户编号"
              width="100">
          </el-table-column>
          <el-table-column
              prop="password"
              label="密码"
              width="100">
          </el-table-column>
          <el-table-column
              prop="role"
              label="权限"
              width="180">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="changeRole(scope.row)">修改权限
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="12">
      </el-col>
    </el-row>
    <el-dialog title="新增用户" :visible.sync="dialogVisible" width="30%">
      <el-form :model="newUserForm" label-width="80px">
        <el-form-item label="密码">
          <el-input v-model="newUserForm.password"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="newUserForm.role" placeholder="请选择权限">
            <el-option
                label="管理员"
                :value="1">
            </el-option>
            <el-option
                label="普通用户"
                :value="2">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>


import {addUser, changeRole, getUserList} from "@/api/app";

export default {
  name: 'UsersView',
  inject: ['commonData'],
  data() {
    return {
      uid:this.commonData.uid,
      role:this.commonData.role,
      dialogVisible: false,
      tableData: [{}],
      newUserForm: {
        password: '',
        role:''
      }
    }
  },
  methods: {
    addNewUser(){
      this.dialogVisible=true;
    },
    getPage() {
      getUserList(this.formInline).then(res => {
        console.log(res)
        this.tableData = res.data.list
      })
    },
    changeRole(row) {
      console.log(row)
      changeRole(row).then(() => {
        this.getPage()
      })
    },
    addUser(){
      console.log(this.newUserForm)
      addUser(this.newUserForm).then(()=>{
        this.getPage()
        this.dialogVisible=false
      })
    }
  },
  created() {
    this.getPage()
  }
}
</script>